<?php

namespace app\privatearea\controller\mall\v1;

use app\common\cachemodel\privatearea\PrivateAreaUserAddress;
use app\common\lib\Util;
use app\privatearea\controller\MallAuth;
use think\response\Json;

class AddressManage extends MallAuth
{
    /**
     * 地址列表
     * @return Json
     * @throws \Exception
     */
    public function index()
    {
        $page = input('page/d', 1);
        if ($page < 1)
        {
            $page = 1;
        }

        $addressData = (new PrivateAreaUserAddress())->getAddressList($this->user['id'], $page);
        $dataList = [];
        foreach ($addressData['data'] as $key => $addressDatum)
        {
            $dataList[] = [
                'id' => $addressDatum['id'],
                'name' => $addressDatum['name'],
                'phone' => $addressDatum['phone'],
                'country' => $addressDatum['country'],
                'province' => $addressDatum['province'],
                'city' => $addressDatum['city'],
                'address_desc' => $addressDatum['address_desc'],
                'is_default' => $addressDatum['is_default'],
            ];
        }

        $addressData['data'] = $dataList;

        return show(config('code.success'), '请求成功', $addressData);
    }

    /**
     * 添加或者编辑地址
     * @return Json
     * @throws \Exception
     */
    public function addOrEdit()
    {
        $id = input('id/d', 0);
        $uid = (int)$this->user['id'];
        $addressData = [
            'name' => input('name/s', ''),
            'phone' => input('phone/s', ''),
            'country' => input('country/s', ''),
            'province' => input('province/s', ''),
            'city' => input('city/s', ''),
            'address_desc' => input('address_desc/s', ''),
            'is_default' => input('is_default/d', 0),
        ];

        if (Util::commentCheck($uid, $addressData))
        {
            return show(config('code.error'), '请求频繁，请稍后再试～');
        }

        $result = (new PrivateAreaUserAddress())->addOrEdir($uid, $id, $addressData);

        if ($result['res'])
        {
            return show(config('code.success'), '请求成功', $result['data']);
        } else
        {
            return show(config('code.error'), $result['msg']);
        }
    }

    public function del()
    {
        $id = input('id/d', 0);
        if (!$id)
        {
            return show(config('code.error'), '请求异常，请稍后再试');
        }
        $privateAreaUserAddressModel = new PrivateAreaUserAddress();

        $info = $privateAreaUserAddressModel->getInfoById($id);
        if (!$info)
        {
            return show(config('code.error'), '删除成功');
        }
        if ($info['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '服务异常，地址信息不存在～');
        }

        $res = $privateAreaUserAddressModel->addressDel($this->user['id'], $id);
        if ($res)
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), '服务异常，删除失败～');
        }
    }
}